<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" class="nav">
      <el-breadcrumb-item to="/welcome"
        ><strong>首页</strong></el-breadcrumb-item
      >
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表 </el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <div class="header">
        <el-input
          placeholder="请输入内容"
          v-model="obj.query"
          class="input-with-select"
          clearable
          @clear="getgoodslist"
        >
          <!--clearable清除输入框内容,@clear调用函数重新发送请求-->
          <el-button
            slot="append"
            @click="getgoodslist"
            icon="el-icon-search"
          ></el-button>
        </el-input>
        <el-button type="primary" @click="addgoods" class="add-btn"
          >添加商品</el-button
        >
      </div>
      <el-table
        class="table"
        border
        :data="goodlist"
        style="width: 100%"
        max-height="500"
      >
        <el-table-column align="center" label="" width="50">
          <template slot-scope="{ $index }">
            {{ obj.pagesize * (obj.pagenum - 1) + $index + 1 }}
          </template>
        </el-table-column>

        <el-table-column prop="goods_name" label="商品名称" width="800px">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px">
        </el-table-column>
        <el-table-column
          :formatter="formatCreateTime"
          prop="add_time"
          label="创建时间"
        >
        </el-table-column>

        <el-table-column label="操作">
          <!-- slot-scope="{ row }" -->
          <template slot-scope="{ row }">
            <div class="actions">
              <el-button type="primary">
                <i class="el-icon-edit-outline"></i
              ></el-button>
              <el-button type="danger">
                <i class="el-icon-delete" @click="del(row)"></i
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="Number(obj.pagenum)"
        :page-sizes="listarr"
        :page-size="Number(obj.pagesize)"
        layout="total, sizes, prev, pager, next, jumper"
        :total="Number(total)"
        style="margin-top: 20px"
        background=""
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { GoodsListApi, delGoodsApi } from '@/api/goods'
export default {
  name: '',
  data () {
    return {
      goodlist: [],
      listarr: [1, 3, 5, 10],
      obj: {
        query: '',
        pagenum: '1',
        pagesize: '10'
      },
      total: '',
    }
  },
  created () {
    this.getgoodslist()
    // console.log(this.$route);
  },
  computed: {
  },
  methods: {
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.obj.pagesize = val
      this.getgoodslist()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.obj.pagenum = val
      this.getgoodslist()
    },
    formatCreateTime (row, column, cellValue) {
      const timestamp = cellValue // 假设 `create_time` 是时间戳
      const date = new Date(timestamp)
      const year = date.getFullYear()
      const month = ('0' + (date.getMonth() + 1)).slice(-2)
      const day = ('0' + date.getDate()).slice(-2)
      const hours = ('0' + date.getHours()).slice(-2)
      const minutes = ('0' + date.getMinutes()).slice(-2)
      const seconds = ('0' + date.getSeconds()).slice(-2)
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    },
    async getgoodslist () {
      const res = await GoodsListApi(this.obj)
      this.goodlist = res.data.goods
      this.total = res.data.total
      console.log(res)
    },
    // 添加商品
    addgoods () {
      console.log(1)
      this.$router.push('/addgoods')
    },
    // 删除商品
    del (row) {
      this.$confirm('此操作将删除商品 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await delGoodsApi(row.goods_id)
        this.getgoodslist()
        this.$message({
          type: 'success',
          message: '删除成功'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>
<style lang='less'  scoped>
.table {
  font-size: 12px;
}
.actions {
  display: flex;
  button {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 43px;
    height: 28px;
  }
}
.nav {
  font-size: 12px;
  margin-bottom: 15px;
}
.header {
  display: flex;
  margin-bottom: 20px;
  .input-with-select {
    width: 420px;
    margin-right: 15px;
  }
}
</style>
